var option = {
    module : "default",
};
var fs = require("fs");
//默认配置
var oUpdateInfo = {
    version : "1.0.0",
    projectName : "CMCC_XDF_Education",
    updateList : [
        {
            path : "js/gate.js",
            desc : "增加页面选择性精准营销初始判断"
        }
    ],
    src : "E:/workCode/HBEducation/CMCCEducation/Education/"
}
//对参数进行整理
for(var i = 2; i < process.argv.length; i+=2){
    if(!isNaN(process.argv[i+1])){
        process.argv[i+1] = parseInt(process.argv[i+1]);
    }
    option[process.argv[i].substring(1)] = process.argv[i+1];
    
}
//读取更新配置文件
var strUpdateConfig = fs.readFileSync("./update.config.json");
var oUpdateConfig = JSON.parse(strUpdateConfig.toString());
if(oUpdateConfig[option.module]){
    oUpdateInfo = oUpdateConfig[option.module];
}
//升级文件夹
var oDate = new Date();
var iYear = oDate.getFullYear();
var iMonth = oDate.getMonth() + 1;
var iDate = oDate.getDate();
strTime = iYear + (iMonth > 9 ? iMonth : "0" + iMonth) + (iDate > 9 ? iDate : "0" + iDate);
//定义生成目录名称格式
var strOuterDirName = oUpdateInfo.projectName + ".v" + oUpdateInfo.version + strTime;
if(!fs.existsSync(strOuterDirName)){
    fs.mkdirSync(strOuterDirName);
}
//更新描述文件
var strDistPath = oUpdateInfo.updateLog || "./updateInfo/"+ (option.module) +"UpdateList.txt";
fs.writeFileSync(strDistPath, oUpdateInfo.projectDesc+":\n");

//更具更新列表生成文件
var iDescCount = 0;
for(let i = 0; i < oUpdateInfo.updateList.length;  i++){
    let oFileDirInfo = oUpdateInfo.updateList[i];
    let strPath = oFileDirInfo.path;
    let strDesc = oFileDirInfo.desc;
    let aPath = strPath.split("/");
    let iCount = 0;
    var strCrumbs = "";
    while(iCount < aPath.length){
        let strRelativePath = strCrumbs +aPath[iCount];
        let strFilePath = strOuterDirName+"/" + strRelativePath;
        if(-1 != aPath[iCount].indexOf(".")){
            //生成并复制文件内容
            var data = fs.readFileSync(oUpdateInfo.src + strRelativePath);
            fs.writeFileSync(strFilePath,data);
            //添加描述
            if(strDesc){
                iDescCount += 1;
                fs.appendFileSync(strDistPath, iDescCount + "." + strDesc + "\n");
            }
            
        }else{
            //生成文件夹
            if(!fs.existsSync(strFilePath)){
                fs.mkdirSync(strFilePath);
            }
        }
        
        strCrumbs += aPath[iCount] + "/";
        iCount++;
    }
}
